# _*_ coding: UTF-8 _*_
# 作者：wu meng
# 创建时间：2020/8/5 15:00
# 创建工具：PyCharm
import os, sys

from selenium.webdriver.chrome.options import Options
from selenium import webdriver
from selenium.webdriver.chrome.service import Service

wb_config = {
    # "chrome_path": "resource\\Google Chrome\\chrome.exe",
    # "chrome_driver_path": "resource\\Google Chrome\\chromedriver.exe",
    # "chrome_path": 'bin/Google Chrome.app/Contents/MacOS/Google Chrome',
    # "chrome_driver_path": "bin/chromedriver_mac64/chromedriver",
    "chrome_path": '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
    "chrome_driver_path": "/Users/apple/Desktop/workspace/python/hanwei-app/bin/chromedriver-mac-x64/chromedriver",
}

def get_webdriver():
    print(f"当前目录: {sys.argv}")
    # realpath = os.path.dirname(os.path.realpath(sys.argv[0]))
    # chrome_path = os.path.join(realpath, wb_config['chrome_path'])
    # chrome_driver_path = os.path.join(realpath, wb_config['chrome_driver_path'])
    chrome_path = wb_config['chrome_path']
    chrome_driver_path = wb_config['chrome_driver_path']
    print(chrome_path,chrome_driver_path)
    chrome_options = Options()  # 网上找到 你可以试试
    chrome_options.binary_location = chrome_path  # 这里是你指定浏览器的路径
    # chrome_options.add_argument("--window-size=1920x1080")
    chrome_options.add_argument('--headless')
    chrome_options.add_argument("disable-cache")  # 禁用缓存
    chrome_options.add_argument('disable-infobars')
    chrome_options.add_argument('--disable-gpu')
    chrome_options.add_argument('--ignore-certificate-errors') # 忽略证书
    chrome_options.add_argument('log-level=3')  # INFO = 0 WARNING = 1 LOG_ERROR = 2 LOG_FATAL = 3 default is 0
    # chrome_options.add_experimental_option('excludeSwitches', ['enable-logging'])
    # return webdriver.Chrome(executable_path=chrome_driver_path, chrome_options=chrome_options)
    service = Service(chrome_driver_path)
    return webdriver.Chrome(service=service, options=chrome_options)


def get_webdriver_header(x=1920,y=1080):
    # realpath = os.path.dirname(os.path.realpath(sys.argv[0]))
    # chrome_path = os.path.join(realpath, wb_config['chrome_path'])
    # chrome_driver_path = os.path.join(realpath, wb_config['chrome_driver_path'])
    chrome_path = wb_config['chrome_path']
    chrome_driver_path = wb_config['chrome_driver_path']
    print(chrome_path,chrome_driver_path)
    chrome_options = Options()  # 网上找到 你可以试试
    chrome_options.binary_location = chrome_path  # 这里是你指定浏览器的路径
    size = 'window-size=' + str(x) + ',' + str(y)
    chrome_options.add_argument(size)
    chrome_options.add_argument('window-position=-1024,-768')
    windowPos = "window-position=-" + str(x) + ",-" + str(y)
    chrome_options.add_argument(windowPos)
    chrome_options.add_argument("disable-cache")  # 禁用缓存
    chrome_options.add_argument('disable-infobars')
    # chrome_options.add_argument('--disable-gpu')
    chrome_options.add_argument('log-level=3')  # INFO = 0 WARNING = 1 LOG_ERROR = 2 LOG_FATAL = 3 default is 0
    # chrome_options.add_experimental_option('excludeSwitches', ['enable-logging'])
    # return webdriver.Chrome(executable_path=chrome_driver_path, chrome_options=chrome_options)
    service = Service(chrome_driver_path)
    return webdriver.Chrome(service=service, options=chrome_options)